package com.cnhis.cloudhealth.clinical.medical.base.bo;


import com.cnhis.cloudhealth.clinical.medical.base.dao.McallDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

@Service
public class McallBo {
    @Resource
    private McallDao mcallDao;
    @Autowired
    private GetNewId getNewId;

    /**
     * @param '[map]'
     * @return java.util.Map<java.lang.String,java.lang.Object>
     * @throws
     * @Description:(病人列表)
     * @author chengshaowei
     * @date 2016/9/19 10:30
     */
    public void save(Map<String, Object> map) throws Exception {
        /**查看此病人是否被呼叫**/
        Map<String,Object> callinfo=mcallDao.findCallInfo(map);
        if(callinfo!=null){
            mcallDao.update(map);
        }else{
            /**获取id**/
            Map<String,Object> vcz1map=mcallDao.findVcz1Id(map);
            Map<String,Object> calculate=new HashMap<String,Object>();
            calculate.put("IncCount",1);
            mcallDao.updateVcz1Id(calculate);
            map.put("VCZ01", getNewId.nextId());/**主键**/

            calculate.put("VDA02",0);
            calculate.put("VDA03",map.get("VCZ02"));
            calculate.put("VDA04",map.get("BCK01"));
            Map<String,Object> map1=mcallDao.findVda1(calculate);
            if(map1!=null){
                mcallDao.updateVda1(calculate);
                map.put("VCZ09", Integer.parseInt(map1.get("vda05").toString())+1);/**科室次序**/
            }else{
                mcallDao.insertVda1(calculate);
                map.put("VCZ09", 1);/**科室次序**/
            }



            calculate.put("VDA02",1);
            Map<String,Object> map2=mcallDao.findVda1(calculate);
            if(map2!=null){
                mcallDao.updateVda1(calculate);
                map.put("VCZ11", Integer.parseInt(map1.get("vda05").toString())+1);/**科室次序**/
            }else{
                mcallDao.insertVda1(calculate);
                map.put("VCZ11", 1);/**科室次序**/
            }
            mcallDao.save(map);
        }
    }

    /**
     * chengshaowei 2017/4/14 14:28
     *
     * @param map
     * @return com.cnhis.cloudhealth.commons.Mappers.ModelVo
     * @throws
     * @Title: infusionCall
     * @Description: （输液呼叫列队
     * 1、查询此病人是否被呼叫
     * 2、未呼叫则插入呼叫列队
     * 3、已呼叫返回呼叫号码）
     */
    public ModelVo infusionCall(Map<String, Object> map) throws Exception {
        ModelVo vo = new ModelVo();
        Map<String, Object> vcz1map2 = mcallDao.findCallInfo2(map);
        DecimalFormat df = new DecimalFormat("0000");
        if (vcz1map2 != null) {
            //病人已被呼叫，返回呼叫信息
            String a = df.format(vcz1map2.get("serialnum"));
            vcz1map2.put("serialnum", a);
            vo.setMap(vcz1map2);
            vo.setResult(StaticKeys.OH_SUCCESS);
            vo.setResultMsg("病人呼叫成功！");
        } else {
            //病人未呼叫，返回呼叫信息
            Map<String, Object> maxmap = mcallDao.findMaxSerialnum(map);
            map.put("VCZ01", getNewId.nextId());/**主键**/
            map.put("VCZ09", 1);/**科室次序**/
            map.put("VCZ11", 1);/**科室次序**/
            map.put("serialnum", Integer.parseInt(maxmap.get("serialnum").toString()) + 1);
            mcallDao.saveInfusionCall(map);
            String a = df.format(map.get("serialnum"));
            map.put("serialnum", a);
            vo.setMap(map);
            vo.setResult(StaticKeys.OH_SUCCESS);
            vo.setResultMsg("病人呼叫成功！");
        }
        return vo;
    }

    public Map<String, Object> findCallInfo2(Map<String, Object> map) throws Exception {
        return mcallDao.findCallInfo2(map);
    }
}
